package com.gmail.drheiniger.book.organizer;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import com.gmail.drheiniger.backend.Book;
import com.gmail.drheiniger.backend.Database;

public class GUI implements ActionListener {
	// constants
	private static final String TITLE_LABEL = "Add Books:";
	private static final String BOOK_TITLE_LABEL = "Book Title:";
	private static final String AUTHOR_LABEL = "Author:";
	private static final String YEAR_READ_LABEL = "Year Read:";
	private static final String RATING_LABEL = "Rating";
	private static final String CONTENT_LABEL = "Content:";
	private static final String BOOK_TITLE_TEXT_FIELD = "Title:";
	private static final String AUTHOR_TEXT_FIELD = "Author:";
	private static final String YEAR_READ_TEXT_FIELD = "Year:";
	private static final String OK_BUTTON = "OK";
	private static final String BACK_BUTTON = "Back";
	private static final String[] RATING_COMBO_BOX = { "<Select>", "Excellent", "Good", "Average", "Bad", "Horrible" };
	private static final String[] CONTENT_COMBO_BOX = { "<Select>", "G Rated", "PG 13", "R" };
	// initialize components
	JLabel titleLabel = new JLabel(TITLE_LABEL);
	JLabel bookTitleLabel = new JLabel(BOOK_TITLE_LABEL);
	JLabel authorLabel = new JLabel(AUTHOR_LABEL);
	JLabel yearReadLabel = new JLabel(YEAR_READ_LABEL);
	JLabel ratingLabel = new JLabel(RATING_LABEL);
	JLabel contentLabel = new JLabel(CONTENT_LABEL);
	JTextField bookTitleTextField = new JTextField(BOOK_TITLE_TEXT_FIELD);
	JTextField authorTextField = new JTextField(AUTHOR_TEXT_FIELD);
	JTextField yearReadTextField = new JTextField(YEAR_READ_TEXT_FIELD);
	JButton okButton = new JButton(OK_BUTTON);
	JButton cancelButton = new JButton(BACK_BUTTON);
	JComboBox<String> ratingComboBox = new JComboBox<>(RATING_COMBO_BOX);
	JComboBox<String> contentCombobox = new JComboBox<>(CONTENT_COMBO_BOX);
	JFrame frame = new JFrame();
	Container contentPane;
	// Constants
	final int BUFFER_LENGTH = 50;

	public GUI() {
		frame = new JFrame("Book Organizer");
		contentPane = frame.getContentPane();
		SpringLayout layout = new SpringLayout();
		contentPane.setLayout(layout);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		bookTitleTextField.addActionListener(this);
		authorTextField.addActionListener(this);
		yearReadTextField.addActionListener(this);
		// add components to frame
		contentPane.add(bookTitleLabel);
		contentPane.add(authorLabel);
		contentPane.add(yearReadLabel);
		contentPane.add(ratingLabel);
		contentPane.add(contentLabel);
		contentPane.add(bookTitleTextField);
		contentPane.add(authorTextField);
		contentPane.add(yearReadTextField);
		contentPane.add(ratingComboBox);
		contentPane.add(contentCombobox);
		contentPane.add(okButton);
		contentPane.add(cancelButton);
		// Adjust constraints for the components
		layout.putConstraint(SpringLayout.WEST, bookTitleLabel, BUFFER_LENGTH, SpringLayout.WEST, contentPane);
		layout.putConstraint(SpringLayout.NORTH, bookTitleLabel, BUFFER_LENGTH, SpringLayout.NORTH, titleLabel);
		layout.putConstraint(SpringLayout.WEST, authorLabel, 0, SpringLayout.WEST, bookTitleLabel);
		layout.putConstraint(SpringLayout.NORTH, authorLabel, BUFFER_LENGTH, SpringLayout.SOUTH, bookTitleLabel);
		layout.putConstraint(SpringLayout.WEST, yearReadLabel, 0, SpringLayout.WEST, bookTitleLabel);
		layout.putConstraint(SpringLayout.NORTH, yearReadLabel, BUFFER_LENGTH, SpringLayout.SOUTH, authorLabel);
		layout.putConstraint(SpringLayout.WEST, ratingLabel, 0, SpringLayout.WEST, bookTitleLabel);
		layout.putConstraint(SpringLayout.NORTH, ratingLabel, BUFFER_LENGTH, SpringLayout.SOUTH, yearReadLabel);
		layout.putConstraint(SpringLayout.WEST, contentLabel, 0, SpringLayout.WEST, bookTitleLabel);
		layout.putConstraint(SpringLayout.NORTH, contentLabel, BUFFER_LENGTH, SpringLayout.SOUTH, ratingLabel);
		layout.putConstraint(SpringLayout.NORTH, bookTitleTextField, 0, SpringLayout.NORTH, bookTitleLabel);
		layout.putConstraint(SpringLayout.WEST, bookTitleTextField, 2 * BUFFER_LENGTH, SpringLayout.EAST, bookTitleLabel);
		layout.putConstraint(SpringLayout.NORTH, authorTextField, 0, SpringLayout.NORTH, authorLabel);
		layout.putConstraint(SpringLayout.WEST, authorTextField, 0, SpringLayout.WEST, bookTitleTextField);
		layout.putConstraint(SpringLayout.EAST, authorTextField, 0, SpringLayout.EAST, bookTitleTextField);
		layout.putConstraint(SpringLayout.NORTH, yearReadTextField, 0, SpringLayout.NORTH, yearReadLabel);
		layout.putConstraint(SpringLayout.WEST, yearReadTextField, 0, SpringLayout.WEST, bookTitleTextField);
		layout.putConstraint(SpringLayout.EAST, yearReadTextField, 0, SpringLayout.EAST, bookTitleTextField);
		layout.putConstraint(SpringLayout.NORTH, ratingComboBox, 0, SpringLayout.NORTH, ratingLabel);
		layout.putConstraint(SpringLayout.WEST, ratingComboBox, 0, SpringLayout.WEST, bookTitleTextField);
		layout.putConstraint(SpringLayout.EAST, ratingComboBox, 0, SpringLayout.EAST, bookTitleTextField);
		layout.putConstraint(SpringLayout.NORTH, contentCombobox, 0, SpringLayout.NORTH, contentLabel);
		layout.putConstraint(SpringLayout.WEST, contentCombobox, 0, SpringLayout.WEST, bookTitleTextField);
		layout.putConstraint(SpringLayout.EAST, contentCombobox, 0, SpringLayout.EAST, bookTitleTextField);
		layout.putConstraint(SpringLayout.NORTH, okButton, BUFFER_LENGTH, SpringLayout.SOUTH, contentLabel);
		layout.putConstraint(SpringLayout.WEST, okButton, 0, SpringLayout.WEST, bookTitleLabel);
		layout.putConstraint(SpringLayout.EAST, okButton, 0, SpringLayout.EAST, yearReadLabel);
		layout.putConstraint(SpringLayout.NORTH, cancelButton, 0, SpringLayout.NORTH, okButton);
		layout.putConstraint(SpringLayout.EAST, cancelButton, 0, SpringLayout.EAST, bookTitleTextField);
		layout.putConstraint(SpringLayout.WEST, cancelButton, 0, SpringLayout.WEST, bookTitleTextField);
		layout.putConstraint(SpringLayout.EAST, contentPane, BUFFER_LENGTH, SpringLayout.EAST, bookTitleTextField);
		layout.putConstraint(SpringLayout.SOUTH, contentPane, BUFFER_LENGTH, SpringLayout.SOUTH, okButton);

		frame.setSize(400, 500);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setResizable(false);
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		Component buttonSource = null;
		String btnSrcAction = "";
		buttonSource = (JButton) (e.getSource());
		btnSrcAction = ((JButton) buttonSource).getText();
		// Component textFieldSource = null;
		// String tVSrcAction = "";
		// textFieldSource = (JTextField) (e.getSource());
		// tVSrcAction = ((JTextField) textFieldSource).getText();
		switch (btnSrcAction) {
		case OK_BUTTON:
			if (ratingComboBox.getSelectedIndex() == 0) {
				JOptionPane.showMessageDialog(frame, "Please enter a valid rating");
				break;
			} else if (contentCombobox.getSelectedIndex() == 0) {
				JOptionPane.showMessageDialog(frame, "Please enter a valid content rating");
				break;
			} else {
				Database db = new Database();
				Book book = new Book();
		//
				book.setTitle(bookTitleTextField.getText());
				book.setAuthor(authorTextField.getText());
				book.setYear(yearReadTextField.getText());
				book.setRating((String) ratingComboBox.getSelectedItem());
				book.setContent((String) contentCombobox.getSelectedItem());
				db.insertBook(book);
				//		
//				db.insertRow(bookTitleTextField.getText(), authorTextField.getText(), yearReadTextField.getText(), (String) ratingComboBox.getSelectedItem(),
//						(String) contentCombobox.getSelectedItem());
			}
			JOptionPane.showMessageDialog(frame, "Book Added");
			bookTitleTextField.setText("");
			authorTextField.setText("");
			yearReadTextField.setText("");
			ratingComboBox.setSelectedIndex(0);
			contentCombobox.setSelectedIndex(0);
			break;
		case BACK_BUTTON:
			// DO CANCEL BUTTON STUFF
			new OverviewGUI();
			frame.dispose();
			break;
		}
	}
	// switch (){
	//
	// case BOOK_TITLE_LABEL:
	// bookTitleTextField.setText("");
	// break;
	//
	// case AUTHOR_LABEL:
	// authorTextField.setText("");
	// break;
	//
	// case YEAR_READ_LABEL:
	// yearReadTextField.setText("");
	// break;
	// }
	//
}